package tree.迭代bfs;

import po.TreeNode;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/**
 * @author songZiHao
 * @version 1.0.0
 * @ClassName 二叉树的前序遍历144.java
 * @Description TODO https://leetcode-cn.com/problems/binary-tree-preorder-traversal/
 * @createTime 2021年04月09日 22:40:00
 */
public class 二叉树的前序遍历144 {

    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> nodes=new ArrayList<>();
        if (root == null) {
            return nodes;
        }
        Stack<TreeNode> stack=new Stack<>();
        stack.push(root);
        while (!stack.isEmpty()){
            TreeNode pop = stack.pop();
            nodes.add(pop.val);
            if (pop.right != null) {
                stack.push(pop.right);
            }
            if (pop.left != null) {
                stack.push(pop.left);
            }
        }
        return nodes;
    }
}
